<?php
/*
	挪车卡列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'所有'),
	array('op'=>'list','title'=>'未绑定','query'=>'status=0'),
	array('op'=>'list','title'=>'已绑定','query'=>'status=1'),
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if($_RQ['number']){
		$params['number']=intval($_RQ['number']);
		$return['params']['number']=$_RQ['number'];
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['remark']){
		$params['remark like']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		
		$time_range=json_decode($time_range,true);
		
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$fields=array('id','agent_id','uniacid','mid','type','number','code','salt','createtime','remark','url','activetime');
	$list=pdo_getslice('jy_move_agentcar_cards',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			//查询用户信息
			if($item['mid']){
				$item['nickname']=pdo_getcolumn('jy_move_agentcar_member',array('mid'=>$item['mid']),'name');
			}else{
				$item['nickname']='未绑定';
			}
			$item['card_number']=$this->card_number($item['number']);
			$item['user_num']=$this->user_number($item['number']);;
			$mid=$this->getcolumn('agent',array('id'=>$item['agent_id']),'mid');
			$item['agent']=cfc('core')->member_show($mid,'/member.list/agent_detail/mid={mid}');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'agent','title'=>'所属代理商','type'=>'member'),
			array('name'=>'card_number','title'=>'挪车卡编号','type'=>'string'),
			array('name'=>'nickname','title'=>'所属用户','type'=>'string'),
			array('name'=>'user_num','title'=>'用户码','type'=>'string'),
			array('name'=>'remark','title'=>'场景备注','type'=>'string'),
			array('name'=>'type','title'=>'二维码类型','type'=>'radio','options'=>'types'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'createtime','title'=>'生成时间','type'=>'time'),
			array('name'=>'activetime','title'=>'激活时间','type'=>'time'),
			array('name'=>'salt','title'=>'加密盐','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'choose_agent','title'=>'划拨给代理'),
		),'batch'=>array(
			array('type'=>'modal','op'=>'choose_agent','title'=>'划拨给代理'),
		)),
		'search'=>array(
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'types'),
			array('name'=>'number','title'=>'挪车卡编号','type'=>'string'),
			array('name'=>'remark','title'=>'场景备注','type'=>'string'),
			array('name'=>'createtime','title'=>'生成时间','type'=>'time_range'),
		),
	);
	$return['types']=array('0'=>'不限','1'=>'含参','2'=>'智能');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	if($_RQ['number']){
		$params['number']=intval($_RQ['number']);
		$return['params']['number']=$_RQ['number'];
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['remark']){
		$params['remark like']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		
		$time_range=json_decode($time_range,true);
		
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		
	}
	$fields=array('id','uniacid','mid','type','number','code','salt','createtime','remark','url');
	$list=pdo_getall('jy_move_agentcar_cards',$params,$fields,'','id desc');
	if($list){
		$types=array('0'=>'不限','1'=>'含参','2'=>'智能');
		foreach($list as &$item){
			//查询用户信息
			if($item['mid']){
				$item['nickname']=pdo_getcolumn('jy_move_agentcar_member',array('mid'=>$item['mid']),'name');
			}else{
				$item['nickname']='未绑定';
			}
			$item['card_number']=$this->card_number($item['number']);
			$item['user_num']=$this->user_number($item['number']);;
			$item['type']=$types[$item['type']];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'card_number'=>'挪车卡编号',
		'nickname'=>'所属用户',
		'user_num'=>'用户码',
		'remark'=>'场景备注',
		'type'=>'二维码类型',
		'url'=>'二维码链接',
		'createtime'=>'生成时间',
	);
	cfc('tools')->export2excel($header,$list,'挪车卡列表');
}
/*
if($_RQ['op']=='change_agent'){
	//修改所属代理商
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post){
			$update=array(
				'agent_id'=>intval($post['agent_id']),
			);
			$params=array();
			if($post['id']){
				$params['id']=intval($post['id']);
			}elseif($post['ids']){
				$params['id']=str_array($post['ids']);
			}else{
				exi('请先选择要划拨的挪车卡！','error');
			}
			$this->update('cards',$update,$params);
			exi('修改成功！','','list');
		}
		
	}
	$item=array(
		'id'=>intval($_RQ['id']),
		'ids'=>trim($_RQ['ids']),
	);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'select','name'=>'agent_id','title'=>'选择代理商','options'=>'agents'),
		),
	);
	$return['agents']=$this->getall('agent',array('uniacid'=>$_SESSION['uniacid']));
	
	foreach($return['agents'] as &$agent){
		$nickname=pdo_getcolumn('core_members',array('id'=>$agent['mid']),'nickname');
		$address=pdo_getcolumn('core_area',array('code'=>$agent['area_code']),'address');
		$agent['title']="{$nickname}（{$address} 代理商）";
	}
	exi($return);
}*/
if($_RQ['op']=='choose_agent'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['pcd']){
		$pcd=cfc('tools')->base64_decode($_RQ['pcd'],$_SESSION['base_key_str']);
		
		$pcd=json_decode($pcd,true);
		$return['params']['pcd']=$pcd;
		//根据选择的省市区查找代理商
		if($pcd['province']){
			//省级代理
			$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$pcd['province']),'code');
			$type=1;
		}
		if($pcd['city']){
			$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$pcd['city']),'code');
			$type=2;
		}
		if($pcd['district']){
			$type=3;
			$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$pcd['district']),'code');
		}
		if($code){
			$codes=array($code);
			if($type==1){
				$c_codes=pdo_getall('core_area',array('parent_code'=>$code),array('code'));
				foreach($c_codes as $c_code){
					$codes[]=$c_code['code'];
					$d_codes=pdo_getall('core_area',array('parent_code'=>$c_code),array('code'));
					if($d_codes){
						foreach($d_codes as $d_code){
							$codes[]=$d_code['code'];
						}
					}
				}
			}
			if($type==2){
				$d_codes=pdo_getall('core_area',array('parent_code'=>$code),array('code'));
				foreach($d_codes as $d_code){
					$codes[]=$d_code['code'];
					
				}
			}
		}
		if($codes){
			$params['area_code']=$codes;
		}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$id=intval($_RQ['id']);
	$return['params']['id']=$id;
	$ids=trim($_RQ['ids']);
	$return['params']['ids']=$ids;
	$list=$this->get_list('agent',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['area_code']=pdo_getcolumn('core_area',array('code'=>$item['area_code']),'address');
			$item['choose']=array('op'=>'choose_agent_res','query'=>"agent_id={$item['id']}&id={$id}&ids={$ids}");
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'member','title'=>'代理商','type'=>'member'),
			array('name'=>'type','title'=>'代理商类型','type'=>'radio','options'=>'types'),
			array('name'=>'area_code','title'=>'代理区域','type'=>'string'),
			array('name'=>'createtime','title'=>'代理时间','type'=>'time'),
			//array('name'=>'stop','title'=>'代理状态','type'=>'radio','options'=>'stops'),
		),'action'=>array(
			array('type'=>'post','name'=>'choose','title'=>'划拨'),
		)),
		'search'=>array(
			//array('type'=>'choose','name'=>'mid','title'=>'代理商','action'=>'member.get_member','plugin'=>'core'),
			array('type'=>'pcd','name'=>'pcd','title'=>'地区','options'=>'areas'),
		),
	);
	$return['htmls']=$htmls;
	$return['areas']=cfc('tools')->pcd();
	exi($return);
}
if($_RQ['op']=='choose_agent_res'){
	$id=intval($_RQ['id']);
	$ids=trim($_RQ['ids']);
	$agent_id=intval($_RQ['agent_id']);
	if(!$agent_id){
		exi('请先选择代理商！');
	}
	$update=array('agent_id'=>$agent_id);
	if($id){
		$params['id']=intval($id);
	}elseif($ids){
		$params['id']=str_array($ids);
	}else{
		exi('请先选择要划拨的挪车卡！','error');
	}
	$this->update('cards',$update,$params);
	exi('划拨成功！','','list');
}
if($_RQ['op']=='del'){
	//删除挪车卡
	$id=intval($_RQ['id']);
	if($id){
		$this->delete('cards',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('cards',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}